import { createWebHashHistory, createRouter } from 'vue-router'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('../views/Home/Index.vue')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login/Index.vue')
  },
  {
    path: '/KB-1',
    name: 'KB-1',
    component: () => import('../views/KB-1/Index.vue')
  },
  {
    path: '/KB-2',
    name: 'KB-2',
    component: () => import('../views/KB-2/Index.vue')
  },
  {
    path: '/KB-3',
    name: 'KB-3',
    component: () => import('../views/KB-3/Index.vue')
  },
  {
    path: '/KB-4',
    name: 'KB-4',
    component: () => import('../views/KB-4/Index.vue')
  },
  {
    path: '/KB-LG',
    name: 'KB-LG',
    component: () => import('../views/KB-LG3/Index.vue')
  },
  {
    path: '/KB-LG1',
    name: 'KB-LG1',
    component: () => import('../views/KB-LG1/Index.vue')
  },
  {
    path: '/KB-LG-2',
    name: 'KB-LG-2',
    component: () => import('../views/KB-LG2/Index.vue')
  },
  {
    path: '/KB_Docking',
    name: 'KB_Docking',
    component: () => import('../views/KB-Docking/Index.vue')
  },
  {
    path: '/KB_WLAN',
    name: 'KB_WLAN',
    component: () => import('@/views/KB-Main/Index.vue')
  },
  {
    path: '/KB_Main',
    name: 'KB_Main',
    component: () => import('@/views/KB-LG-Main/Index.vue')
  },
  {
    path: '/KB_Sub',
    name: 'KB_Sub',
    component: () => import('@/views/KB-Sub/Index.vue')
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  const user = localStorage.getItem('GREAT_WALL_USER')
  if (user || to.path === '/login') {
    next()
  } else {
    // 如果需要登录且用户未登录，跳转到登录页
    next('/login')
  }
})

export default router
